package com.fs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fs.common.PasswordUtils;
import com.fs.mapper.UserMapper;
import com.fs.pojo.User;
import com.fs.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public List<User> findAll() {
        //构建查询条件对象 没有加添加 默认查询所有数据
        QueryWrapper queryWrapper=new QueryWrapper();
        List<User> users=   userMapper.selectList(queryWrapper);
        return users;
    }

    @Override
    public User findById(Integer id) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("id",id);
        User user = userMapper.selectOne(queryWrapper);
        return user;
    }

    @Override
    public int changeNick(String userid,String nickName) {
        Integer id = Integer.valueOf(userid);
        User user = findById(id);
        user.setUsername(nickName);
        int i = userMapper.updateById(user);
        return i;
    }

    @Override
    public int changeImage(String userid, String faceImage) {
        Integer id = Integer.valueOf(userid);
        User user = findById(id);
        user.setFaceImage(faceImage);
        int i = userMapper.updateById(user);
        return i;
    }

    @Override
    public User login(String username, String password) {//明文ddd
        //1 根据用户名获取用户信息
        //创建条件对象
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("username",username);
        User user = userMapper.selectOne(queryWrapper);
        if(user == null){
            return null;
        }
        //2 判断密码是否正确
        String password1 = PasswordUtils.generate(password);
        boolean b = PasswordUtils.verify(user.getPassword(),password1);
        if(b==true){
            return user;
        }else{
            return null;
        }
    }

    @Override
    public User regist(String username, String password) {
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        int insert = userMapper.insert(user);
        if(user == null){
            return null;
        }
        return user;
    }
}
